﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using myUserManagement;

namespace POS
{
    public partial class frmUser : Form
    {
        frmMain parent;
        
        ghieUserManagement ghieUser = new ghieUserManagement();
        int i = 0; 
        DataTable tbl = new DataTable();

        public frmUser(frmMain p)
        {
            InitializeComponent();
            parent = p;
            this.Load += new EventHandler(frmUser_Load);
            btnAdd.Click += new EventHandler(btnAdd_Click);
            btndelete.Click += new EventHandler(btndelete_Click);
            btnupdate.Click += new EventHandler(btnupdate_Click);
            btnsearch.Click += new EventHandler(btnsearch_Click);
            btnadmin.Click += new EventHandler(btnadmin_Click);
            btnuser.Click += new EventHandler(btnuser_Click);

        }

        void btnuser_Click(object sender, EventArgs e)
        {
            ghieUser.connection();
            ghieUser.loadData("Select distinct fName,fUsername,flevel,fStamp from tbluser where flevel ='User' ");
            dataGridView1.DataSource = ghieUser.tbl;
            updateclear();
            ghieUser.DBClose();
        }

        void btnadmin_Click(object sender, EventArgs e)
        {
            ghieUser.connection();
            ghieUser.loadData("Select distinct fName,fUsername,flevel,fStamp from tbluser where flevel ='Admin' ");
            dataGridView1.DataSource = ghieUser.tbl;
            updateclear();
            ghieUser.DBClose();
        }

        void btnsearch_Click(object sender, EventArgs e)
        {

            if (txtSearch.Text == "")
            {
                MessageBox.Show("No Name has been Input", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            ghieUser.connection();
            ghieUser.loadData("SELECT * FROM tbluser WHERE fName LIKE '%" + txtSearch.Text + "%'");
            if (ghieUser.tbl.Rows.Count > 0)
            {
                tbl = ghieUser.tbl;
                txtupname.Text = tbl.Rows[i][1].ToString();
                txtUpusername.Text = tbl.Rows[i][2].ToString();
                txtUppassword.Text = tbl.Rows[i][3].ToString();
                cmblevel.Text = tbl.Rows[i][4].ToString();
                ghieUser.loadData("Select distinct fName,fUsername,flevel,fStamp from tbluser where fName LIKE '%" + txtSearch.Text + "%'");
                dataGridView1.DataSource = ghieUser.tbl;
            }
            else
            {
                MessageBox.Show("No user has been Found", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

            }
            ghieUser.DBClose();
        }

        void btnupdate_Click(object sender, EventArgs e)
        {
            if (txtupname.Text != "" && txtUpusername.Text != "" && txtUppassword.Text != "" && cmblevel.Text != "")
            {
                ghieUser.connection();
                ghieUser.UpdatePassword("Update tbluser set fName='" + this.txtupname.Text + "',fUsername ='" + this.txtUpusername.Text + "',fPassword ='" + this.txtUppassword.Text + "',fLevel = '" + this.cmblevel.Text + "' Where fName='" + this.txtSearch.Text + "';");
                MessageBox.Show("Update success!");
                ghieUser.loadData("Select distinct fName,fUsername,flevel,fStamp from tbluser");
                DataView dw = new DataView(ghieUser.tbl);
                dw.RowFilter = string.Format("fUsername LIKE '%{0}'", txtSearch.Text);
                dataGridView1.DataSource = dw;
                updateclear();
                ghieUser.DBClose();
            }
            else
            {
                MessageBox.Show("Please check the information to update!", "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        void btndelete_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Are you sure to Delete this account ? ", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                ghieUser.connection();
                ghieUser.loadData("DELETE FROM tbluser WHERE fName ='" + txtSearch.Text + "'");
                ghieUser.loadData("Select distinct fName,fUsername,flevel,fStamp from tbluser");
                MessageBox.Show("Account Deleted", "Sucessfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DataView dw = new DataView(ghieUser.tbl);
                dw.RowFilter = string.Format("fUsername LIKE '%{0}'", txtSearch.Text);
                dataGridView1.DataSource = dw;
                updateclear();
                ghieUser.DBClose();


            }
            else
            {
                this.Refresh();

            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            if (optAdmin.Checked)
            {
                ghieUser.connection();
                ghieUser.AddData("INSERT INTO tbluser(fName,fUsername,fPassword,flevel)" + "Values('" + txtname.Text + "','" + txtuser.Text + "','" + txtpass.Text + "','" + optAdmin.Text + "')");
                ghieUser.loadData("Select * from tbluser");
                
                Clear();
                ghieUser.DBClose();
            }
            else if (optUser.Checked)
            {
                ghieUser.connection();
                ghieUser.AddData("INSERT INTO tbluser(fName,fUsername,fPassword,flevel)" + "Values('" + txtname.Text + "','" + txtuser.Text + "','" + txtpass.Text + "','" + optguest.Text + "')");
                ghieUser.loadData("Select * from tbluser");
                
                Clear();
                ghieUser.DBClose();
            }
            else
            {
                MessageBox.Show("Please select type of account", "Type of account", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        void frmUser_Load(object sender, EventArgs e)
        {
        }

        void updateclear()
        {
            txtSearch.Clear();
            txtupname.Clear();
            txtUpusername.Clear();
            txtUppassword.Clear();
            txtconfirmpass.Clear();
            cmblevel.Text = "";

        }

        void Clear()
        {
            
            txtname.Text = "";
            txtpass.Text = "";
            txtuser.Text = "";
            optguest.Checked = false;
            optAdmin.Checked = false;

        }
    }
}
